package com.ruoyi.system.service.impl.vip;

import java.math.BigDecimal;
import java.time.Year;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.domain.model.VsUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.TimeStampUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.user.VsDramasUserWalletLog;
import com.ruoyi.system.domain.vip.VsDramasVipOrder;
import com.ruoyi.system.mapper.user.VsDramasUserWalletLogMapper;
import com.ruoyi.system.mapper.user.VsUserMapper;
import com.ruoyi.system.mapper.vip.VsDramasVipOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.vip.VsDramasVipMapper;
import com.ruoyi.system.domain.vip.VsDramasVip;
import com.ruoyi.system.service.vip.IVsDramasVipService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户充值会员价格Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-24
 */
@Service
public class VsDramasVipServiceImpl implements IVsDramasVipService 
{
    @Autowired
    private VsDramasVipMapper vsDramasVipMapper;

    @Autowired
    private VsUserMapper vsUserMapper;

    @Autowired
    private VsDramasVipOrderMapper vsDramasVipOrderMapper;

    /**
     * 查询用户充值会员价格
     * 
     * @param id 用户充值会员价格主键
     * @return 用户充值会员价格
     */
    @Override
    public VsDramasVip selectVsDramasVipById(Integer id)
    {
        return vsDramasVipMapper.selectVsDramasVipById(id);
    }

    /**
     * 查询用户充值会员价格列表
     * 
     * @param vsDramasVip 用户充值会员价格
     * @return 用户充值会员价格
     */
    @Override
    public List<VsDramasVip> selectVsDramasVipList(VsDramasVip vsDramasVip)
    {
        return vsDramasVipMapper.selectVsDramasVipList(vsDramasVip);
    }

    /**
     * 新增用户充值会员价格
     * 
     * @param vsDramasVip 用户充值会员价格
     * @return 结果
     */
    @Override
    public int insertVsDramasVip(VsDramasVip vsDramasVip)
    {
        vsDramasVip.setCreatetime(System.currentTimeMillis());
        vsDramasVip.setUpdatetime(System.currentTimeMillis());
        return vsDramasVipMapper.insertVsDramasVip(vsDramasVip);
    }

    /**
     * 修改用户充值会员价格
     * 
     * @param vsDramasVip 用户充值会员价格
     * @return 结果
     */
    @Override
    public int updateVsDramasVip(VsDramasVip vsDramasVip)
    {
        vsDramasVip.setUpdatetime(System.currentTimeMillis());
        return vsDramasVipMapper.updateVsDramasVip(vsDramasVip);
    }

    /**
     * 批量删除用户充值会员价格
     * 
     * @param ids 需要删除的用户充值会员价格主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasVipByIds(Integer[] ids)
    {
        return vsDramasVipMapper.deleteVsDramasVipByIds(ids);
    }

    /**
     * 删除用户充值会员价格信息
     * 
     * @param id 用户充值会员价格主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasVipById(Integer id)
    {
        return vsDramasVipMapper.deleteVsDramasVipById(id);
    }

    @Transactional
    @Override
    public int submitVip(VsDramasVip vsDramasVip) {
        long l = System.currentTimeMillis();
        VsDramasVip dramasVip = vsDramasVipMapper.selectVsDramasVipById(vsDramasVip.getId());
        VsUser user = vsUserMapper.selectVsUserById(vsDramasVip.getUserId());
        VsDramasVipOrder order = new VsDramasVipOrder();
        order.setVipId(dramasVip.getId());
        order.setUserId(Math.toIntExact(user.getId()));
        order.setLangId(dramasVip.getLangId());
        String s = "A" + l + IdUtils.fastSimpleUUID();
        order.setOrderSn(s);
        if ("周".equals(dramasVip.getType())) {
            int num = Integer.parseInt(dramasVip.getNum());
            int result = 7 * num;
            long timeStampByDate = TimeStampUtils.getTimeStampByDate(result);
            order.setTimes(timeStampByDate);
        } else if ("月".equals(dramasVip.getType())) {
            int num = Integer.parseInt(dramasVip.getNum());
            int result = 30 * num;
            long timeStampByDate = TimeStampUtils.getTimeStampByDate(result);
            order.setTimes(timeStampByDate);
        } else if ("年".equals(dramasVip.getType())) {
            int num = Integer.parseInt(dramasVip.getNum());
            // 获取当前年份
            int currentYear = Year.now().getValue();
            // 由于每年的天数取决于是否是闰年，可以通过以下方式计算
            boolean isLeapYear = Year.isLeap(currentYear);
            int daysInYear = isLeapYear ? 366 : 365;
            int result = daysInYear * num;
            long timeStampByDate = TimeStampUtils.getTimeStampByDate(result);
            order.setTimes(timeStampByDate);
        }
        long times = order.getTimes();
        order.setStatus(2L);
        order.setTotalFee(dramasVip.getPrice());
        order.setPayFee(BigDecimal.valueOf(0));
        order.setPaytime(l);
        order.setPayType("system");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("expired_time", l + times);
        // 将 JSONObject 转换为 JSON 字符串
        String jsonString = jsonObject.toJSONString();
        order.setExt(jsonString);
        order.setPlatform("H5");
        order.setCreatetime(l);
        order.setUpdatetime(l);
        vsDramasVipOrderMapper.insertVsDramasVipOrder(order);
        VsUser vsUser = new VsUser();
        vsUser.setId(user.getId());
        vsUser.setVipExpiretime(times);
        return vsUserMapper.updateVsUser(vsUser);
    }
}
